<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*"%>
<%@page import="com.hp.hpl.jena.query.*" %>
<%@page import="com.hp.hpl.jena.rdf.model.*" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<%@ include file="/includes/head.jsp" %>

<% 
	com.hp.hpl.jena.query.ResultSet results = (com.hp.hpl.jena.query.ResultSet)request.getAttribute("Results"); 
	String prjName = (String)request.getParameter("PrjName"); 
	String usrQuery = (String)request.getParameter("UserQuery");
	String rsTitle = (String)request.getParameter("ResultTitle");
	String tableSize = (String)request.getParameter("TableSize");
	int numSubmit = Integer.parseInt((String)request.getParameter("NumSubmit"));
	ArrayList<String> arrayList = new ArrayList<String>();
%>

<% 
	ArrayList<String> titleList = new ArrayList<String>();
	Map rsMap = new HashMap();
	if(!rsTitle.equals("")){
		while(rsTitle.length()!=0){
			titleList.add(rsTitle.substring(rsTitle.indexOf('?')+1, rsTitle.indexOf('|')));
			rsTitle = rsTitle.substring(rsTitle.indexOf('|')+1, rsTitle.length());
		}
	}
	
	for(int i=0; i<titleList.size(); i++){
		rsMap.put(titleList.get(i), new ArrayList<String>());
	}

	if(results != null){
		while(results.hasNext()){
			QuerySolution qs = (QuerySolution)results.next();
			
			for (Iterator<String> it = qs.varNames(); it.hasNext(); ) {
				String v = it.next();
				String r = "";
				
				if(qs.get(v) instanceof Literal){
					Literal l = (Literal) qs.get(v);
					r = l.getString();
				}
				else if(qs.get(v) instanceof Resource){
					Resource res = (Resource) qs.get(v);
					r = res.toString();
				}
				
				((ArrayList<String>)rsMap.get(v)).add(r);
			}
		}
}
%>

<body>

<div id="page">

	<%@ include file="/includes/header.jsp" %>
	
	<div id="menulinks">
		<a href="index.jsp"><span>Home</span></a>	
		<div class="menulines"></div>
		<a href="about.jsp"><span>About</span></a>
		<div class="menulines"></div>
		<a href="entry.jsp"><span>Entry</span></a>
		<div class="menulines"></div>
		<a class="active" href="query.jsp"><span>Query</span></a>
		<div class="menulines"></div>
		<% if(null == session.getAttribute("username")) { %>
			<a href="login.jsp"><span>Login</span></a>
		<% } else { %>  
			<a href="logout.jsp"><span>Logout</span></a>
		<% } %>
		<div class="menulines"></div>
		<a href="engineers.jsp"><span>Engineers</span></a>
		<div class="menulines"></div>
	</div>
	
	<div id="mainarea">
		<div id="contentarea">
			<h2><img src="images/process_sm.png" alt="Search Results" /> &nbsp; SPARQL Search Results</h2>
			<div class="content">

				<div>&nbsp;<i>Current location:</i>&nbsp;&nbsp;<a href="query.jsp">Project Select</a>&nbsp;&nbsp;>&nbsp;&nbsp;<a href="ExtractorServlet?PrjName=<%=prjName %>">SPARQL Search</a>&nbsp;&nbsp;>&nbsp;&nbsp;<b>Query Result</b></div>
				
				<p><strong><font size="4">User Query</font></strong></p>
					<blockquote>
						<table border="1">
							<tr><td><B>Subject</B></td><td><B>Predicate</B></td><td><B>Object</B></td></tr> 
							<% 
								if(usrQuery != null && !usrQuery.equals("")){
									while(usrQuery.length() != 0){
										String triple = usrQuery.substring(0, usrQuery.indexOf(';'));
										String subject = triple.substring(0, triple.indexOf('-'));
										String predicate = triple.substring(triple.indexOf('-')+1, triple.lastIndexOf('-'));
										String object = triple.substring(triple.lastIndexOf('-')+1, triple.length());
							%>
									<tr>
										<% if (subject.length() == 0 || subject.equals("()")) { %>
											<td>None</td>
										<% } else { %>	
										<td><%=subject %></td>
										<% } %>
										<% if (predicate.length() == 0) { %>
											<td>None</td>
										<% } else { %>
										<td><%=predicate %></td>
										<% } %>
										<% if (object.length() == 0 || object.equals("()")) { %>
											<td>None</td>
										<% } else { %>
											<td><%=object %></td>
										<% } %>
									</tr>
							<%			
										usrQuery = usrQuery.substring(usrQuery.indexOf(';')+1, usrQuery.length());
									}
								}
							%>
						</table>
					</blockquote>
					<p><strong><font size="4">Query Result</font></strong></p>
					<blockquote>
					<% 	ArrayList<String> defaultList = (ArrayList<String>)rsMap.get(titleList.get(0));
						int size = 0;
						if (defaultList.size() == 0) { %>
							<table cellspacing="1" cellpadding="1" width="200" border="1">
							<tbody>
							<tr>	
							<td>No Results Found ...</td></tr>
							</tbody>
							</table>
					<% } else {
							for (int i=0; i<numSubmit; i++) {
							int start = i*size;
							size = Integer.parseInt(Character.toString(tableSize.charAt(i))); 
							%>
							<table cellspacing="1" cellpadding="1" width="200" border="1">
							<tbody>
							<tr>	
							<% for(int j=start; j<size+start; j++){ %>
								<td><B><%=titleList.get(j).substring(0, titleList.get(j).indexOf('_')) %></B></td>
							<% } %>
							</tr>
							<% if (size == 1) { 
								arrayList.clear();
								for(int k=0; k<defaultList.size(); k++){
									for(int l=start; l<size+start; l++){
										arrayList.add(((ArrayList<String>)rsMap.get(titleList.get(l))).get(k));
										HashSet h = new HashSet(arrayList);
										arrayList.clear();
										arrayList.addAll(h);
									}		
								}
								for (int m=0; m<arrayList.size(); m++) { %>
									<tr><td><%=arrayList.get(m)%> </td></tr>
								<% }
							} else { 
								for(int k=0; k<defaultList.size(); k++){ %>
								<tr>
									<% for(int l=start; l<size+start; l++){ %>
										<td><%=((ArrayList<String>)rsMap.get(titleList.get(l))).get(k) %></td>
									<% } %>
								</tr>		
								<% }
							} %>							
							</tbody>
							</table>
							<br />
							<% }
						} %>
					</blockquote>
				<a href="#header">Back To Top</a>
				
			</div>
		</div>
		<%@ include file="/includes/sidebar.jsp" %>
		<%@ include file="/includes/footer.jsp" %>
	</div>
	
</body>
</html>